var JS_ADAD = function(){
	var owner = this;
	owner.dom_tag = "adad";
	owner.dom_search = $("#"+owner.dom_tag+"_div_search");
	owner.dom_edit = $("#"+owner.dom_tag+"_div_add_edit");
	owner.dom_table = $("#"+owner.dom_tag+"_table");
	owner.currentId = null;
	owner.currentRow = 0;
	owner.sortName = "addTime";
	owner.sortOrder = false;
	owner.map_data = {};
	owner.box_show = [{text:"静态",value:0}, {text:"动态",value:1}];
	owner.box_position = [{text:"第一层",value:"level-10"}, {text:"第二层",value:"level-20"}, {text:"第三层",value:"level-30"}, {text:"第四层",value:"level-40"}];
	owner.isShowSearchWindow = false;
	owner.isShowEidtWindow = false;
	
	
	owner.dom_table.datagrid({
		onSortColumn: function(sort, order){
			owner.sortName = sort;
			owner.sortOrder = true?(order=="asc"):false;
			owner.getPage(1);
		}
	}).datagrid("getPager").pagination({
	    layout: ["list","sep","first","prev","sep","manual","sep","next","last","refresh","sep"],
	    pageList: [10,20,50,100],
	    pageSize: 20,
	    buttons: [{
	        iconCls:"icon-search",
	        handler:function(){
	            owner.dom_search.window("open").window("hcenter");
	            if(!owner.isShowSearchWindow) {
	            	$("#ipt_"+owner.dom_tag+"_search_begin").Zebra_DatePicker({
						pair: $("#ipt_"+owner.dom_tag+"_search_end")
					});
					$("#ipt_"+owner.dom_tag+"_search_end").Zebra_DatePicker({
						direction: true
					});
					$("#ipt_"+owner.dom_tag+"_search_position").combobox("loadData", owner.box_position);
					owner.isShowSearchWindow = true;
	            }
	        }
	    },{
	        iconCls:"icon-edit",
	        handler:function(){
	        	if(owner.dom_table.datagrid("getChecked").length!=1){
	        		$.messager.alert("提示信息", "必须仅选择一条数据进行编辑！");
	        		return;
	        	}
	        	var row = owner.dom_table.datagrid("getChecked")[0];
	        	owner.currentId = row.itemId;
	        	owner.currentRow = owner.dom_table.datagrid("getRowIndex", row);
	            owner.dom_edit.window({
	            	"iconCls": "icon-edit",
	            	"title": "编辑广告信息"
	            }).window("open").window("hcenter");
	            owner.resetSaveOrEdit();
	        }
	    },{
	        iconCls:"icon-add",
	        handler:function(){
	            owner.currentId = null;
	            owner.dom_edit.window({
	            	"iconCls": "icon-add",
	            	"title": "添加广告信息"
	            }).window("open").window("hcenter");
	            owner.resetSaveOrEdit();
	        }
	    },{
	        iconCls:"icon-remove",
	        handler:function(){
	        	var rows = owner.dom_table.datagrid("getChecked");
	            if(rows.length<1){
	        		$.messager.alert("提示信息", "必须选择至少一条数据！");
	        		return;
	        	}
	        	$.messager.confirm("提示信息","确定删除这些数据?",function(isDel){
				    if(isDel) {
				    	var ids = new Array();
				    	for (var i=0; i < rows.length; i++) {
						  ids.push(rows[i].itemId);
						};
						$.ajax({
							type : "post",
							url : "/manager/business/adad/del",
							cache : false,
							dataType : "json",
							data: {
								"adadIds": ids
							}
						}).done(function(result){
							if(result.isTrue) {
								for (var i=0; i < rows.length; i++) {
									owner.dom_table.datagrid("deleteRow", owner.dom_table.datagrid("getRowIndex", rows[i]));
								};
							} else {
								$.messager.alert("提示信息", result.message);
							}
						}).fail(function(){
							$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
						});
				    }
				});
	        }
	    }],
	    onSelectPage: function(pageNum, pageSize){
	    	owner.getPage(pageNum, pageSize);
	    }
	}); 
	
	owner.init = function(){
		if(window.localStorage.getItem("adminRoleName")!="超级管理员") {
			$("#ipt_"+owner.dom_tag+"_search_company").parent().hide();
			owner.dom_table.datagrid("hideColumn", "company");
		}
        $("#"+owner.dom_tag+"_info_ios_upload_ipt").filebox({
            onChange: function() {
                owner.changePreView('ios');
            }
        });
        $("#"+owner.dom_tag+"_info_android_upload_ipt").filebox({
            onChange: function() {
                owner.changePreView('android');
            }
        });
	};
	
	owner.resetSearch = function(){
		owner.dom_search.find(".easyui-textbox").textbox("clear")
		.end().find(".easyui-combobox").combobox("clear")
		.end().find(".easyui-combotree").combotree("clear")
		.end().find(".easyui-numberbox").numberbox("clear")
		.end().find(".textbox").val("");
	};
	
	owner.formatItem = function(obj){
		var item = {
			"itemId": obj.adId,
			"company": obj.adCompanyName,
			"name": obj.adName,
			"owner": obj.adOwnerName,
			"adType": obj.adTypeName,
			"position": obj.adPositionName,
			"tarUrl": obj.adTarUrl,
			"showInfo": "<a href=\"javascript:jsAdad.showAdInfo('"+obj.adId+"');\">点击查看</a>",
//			"showTime": obj.adShowTime,
//			"showType": obj.adShowTypeName,
			"beginTime": obj.adBegin.date(),
			"endTime": obj.adEnd.date(),
			"addTime": obj.addTime.date(),
			"updateTime": obj.updateTime.date()
		};
		return item;
	};
	
	
	owner.getPage = function(pageNum, pageSize){
		if(pageSize==null) {
			pageSize = owner.dom_table.datagrid("getPager").pagination("options").pageSize;
		}
		var params = {};
		if(!owner.isShowSearchWindow) {
			params = {
				"searchCompany": "", 
				"searchName": "", 
				"searchOwner": "", 
				"searchPosition": "", 
				"searchType": "", 
				"searchBegin": "",
				"searchEnd": "",
				"pageNum": pageNum,
				"pageSize": pageSize,
				"orderName": owner.sortName,
				"orderFlag": owner.sortOrder
			};
		} else {
			params = {
				"searchCompany": $("#ipt_"+owner.dom_tag+"_search_company").textbox("getValue"), 
				"searchName": $("#ipt_"+owner.dom_tag+"_search_name").textbox("getValue"), 
				"searchOwner": $("#ipt_"+owner.dom_tag+"_search_owner").textbox("getValue"), 
				"searchPosition": $("#ipt_"+owner.dom_tag+"_search_position").combobox("getValue"), 
//				"searchType": $("#ipt_"+owner.dom_tag+"_search_type").combobox("getValue"), 
				"searchBegin": $("#ipt_"+owner.dom_tag+"_search_begin").val(),
				"searchEnd": $("#ipt_"+owner.dom_tag+"_search_end").val(),
				"pageNum": pageNum,
				"pageSize": pageSize,
				"orderName": owner.sortName,
				"orderFlag": owner.sortOrder
			};
		}
		$.ajax({
			type : "get",
			url : "/manager/business/adad/list",
			cache : false,
			dataType : "json",
			data: params
		}).done(function(result){
			if(result.isTrue) {
				var ps = result.ps;
				var data = new Array();
				owner.map_data = {};
				if(ps.items.length==0) {
					$.messager.alert("提示信息", "暂无数据！");
					return;
				}
				for(var i=0; i<ps.items.length; i++) {
					var obj = ps.items[i];
					owner.map_data[obj.adId] = obj;
					data.push(owner.formatItem(obj));
				}
				owner.dom_table.datagrid("loadData", {
					"total": ps.totalCount,
					"rows": data
				}); 
			} else {
				$.messager.alert("提示信息", result.message);
			}
		}).fail(function(){
			$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
		});
	};
	
	owner.resetSaveOrEdit = function(){
        if(!owner.isShowEidtWindow) {
        	$("#"+owner.dom_tag+"_ipt_beginTime").Zebra_DatePicker({
				pair: $("#"+owner.dom_tag+"_ipt_endTime"),
			});
			$("#"+owner.dom_tag+"_ipt_endTime").Zebra_DatePicker({
				direction: true
			});
			$("#"+owner.dom_tag+"_ipt_position").combobox("loadData", owner.box_position);
//			$("#"+owner.dom_tag+"_ipt_type").combobox("loadData", GLOBAL.getSysTypeMatchList());
//			$("#"+owner.dom_tag+"_ipt_showType").combobox("loadData", owner.box_show);
            $("#"+owner.dom_tag+"_ipt_owner").combobox("loadData", GLOBAL.getOwnerMatchList());
			owner.isShowEidtWindow = true;
        }
		if(owner.currentId==null) {
			owner.dom_edit.find(".easyui-textbox").textbox("clear")
			.end().find(".easyui-combobox").combobox("clear")
			.end().find(".easyui-combotree").combotree("clear")
			.end().find(".easyui-numberbox").numberbox("clear")
			.end().find(".textbox").val("");
		} else {
			var item = owner.map_data[owner.currentId];
			$("#"+owner.dom_tag+"_ipt_name").textbox("setValue", item.adName);
			$("#"+owner.dom_tag+"_ipt_owner").combobox("setValue", item.adOwner);
			$("#"+owner.dom_tag+"_ipt_position").combobox("setValue", item.adPosition);
			$("#"+owner.dom_tag+"_ipt_url").textbox("setValue", item.adTarUrl);
//			$("#"+owner.dom_tag+"_ipt_type").combobox("setValue", item.adType);
//			$("#"+owner.dom_tag+"_ipt_androidUrl").textbox("setValue", item.adAndroidUrl);
//			$("#"+owner.dom_tag+"_ipt_androidDesc").textbox("setValue", item.adAndroidInfo);
//			$("#"+owner.dom_tag+"_ipt_iosUrl").textbox("setValue", item.adIosUrl);
//			$("#"+owner.dom_tag+"_ipt_iosDesc").textbox("setValue", item.adIosInfo);
//			$("#"+owner.dom_tag+"_ipt_showTime").numberbox("setValue", item.adShowTime);
//			$("#"+owner.dom_tag+"_ipt_showType").combobox("setValue", item.adShowType);
			$("#"+owner.dom_tag+"_ipt_beginTime").val(item.adBegin.date(2));
			$("#"+owner.dom_tag+"_ipt_endTime").val(item.adEnd.date(2));
		}
	};
	
	owner.saveOrEdit = function(dom){
		dom.linkbutton("disable");
		var isAllValid = new Array();
		owner.dom_edit.find(".easyui-textbox").each(function(){
			if(!$(this).textbox("isValid")){
				isAllValid.push($(this).prev("label").html());
			}
		});
		owner.dom_edit.find(".easyui-numberbox").each(function(){
			if(!$(this).numberbox("isValid")){
				isAllValid.push($(this).prev("label").html());
			}
		});
		owner.dom_edit.find(".easyui-combobox").each(function(){
			if(!$(this).combobox("isValid")){
				isAllValid.push($(this).prev("label").html());
			}
		});
		if($("#"+owner.dom_tag+"_ipt_beginTime").val().trim()=="") {
			$("#"+owner.dom_tag+"_ipt_beginTime").addClass("err");
			isAllValid.push($("#"+owner.dom_tag+"_ipt_beginTime").prev("label").html());
		} else {
			$("#"+owner.dom_tag+"_ipt_beginTime").removeClass("err");
		}
		if($("#"+owner.dom_tag+"_ipt_endTime").val().trim()=="") {
			$("#"+owner.dom_tag+"_ipt_endTime").addClass("err");
			isAllValid.push($("#"+owner.dom_tag+"_ipt_endTime").prev("label").html());
		} else {
			$("#"+owner.dom_tag+"_ipt_endTime").removeClass("err");
		}
		if(isAllValid.length>0){
			$.messager.alert("提示信息", isAllValid.join(",")+" 输入不正确！");
			dom.linkbutton("enable");
			return;
		}
		$.ajax({
			type : "post",
			url : "/manager/business/adad/addoredit",
			cache : false,
			dataType : "json",
			data: {
				"adId": owner.currentId,
				"adOwner": $("#"+owner.dom_tag+"_ipt_owner").combobox("getValue"), 
				"adName": $("#"+owner.dom_tag+"_ipt_name").textbox("getValue"), 
				"adPosition": $("#"+owner.dom_tag+"_ipt_position").combobox("getValue"), 
				"adTarUrl": $("#"+owner.dom_tag+"_ipt_url").textbox("getValue"), 
//				"adType": $("#"+owner.dom_tag+"_ipt_type").combobox("getValue"), 
//				"adAndroidUrl": $("#"+owner.dom_tag+"_ipt_androidUrl").textbox("getValue"), 
//				"adAndroidInfo": $("#"+owner.dom_tag+"_ipt_androidDesc").textbox("getValue"), 
//				"adIosUrl": $("#"+owner.dom_tag+"_ipt_iosUrl").textbox("getValue"), 
//				"adIosInfo": $("#"+owner.dom_tag+"_ipt_iosDesc").textbox("getValue"), 
				"adShowTime": 3000, 
//				"adShowType": $("#"+owner.dom_tag+"_ipt_showType").combobox("getValue"), 
				"adBegin": $("#"+owner.dom_tag+"_ipt_beginTime").val(), 
				"adEnd": $("#"+owner.dom_tag+"_ipt_endTime").val()
			}
		}).done(function(result){
			if(result.isTrue) {
				var item = result.tag;
				if(owner.currentId==null) {
					owner.dom_table.datagrid("appendRow", owner.formatItem(item));
				} else {
					owner.dom_table.datagrid("updateRow", {
						index: owner.currentRow,
						row: owner.formatItem(item)
					});
				}
				owner.map_data[item.adId] = item;
				owner.dom_edit.window("close");
			} else {
				$.messager.alert("提示信息", result.message);
			}
		}).fail(function(){
			$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
		}).complete(function(){
			dom.linkbutton("enable");
		});
	};
	
	
	/*****************	showAdInfo field************************************/
	owner.showAdInfo = function(adId){
		owner.currentId = adId;
		var obj = owner.map_data[adId];
//		$("#"+owner.dom_tag+"_info_androidUrl").html(obj.adAndroidUrl);
//		$("#"+owner.dom_tag+"_info_androidDesc").html(obj.adAndroidInfo);
//		$("#"+owner.dom_tag+"_info_iosUrl").html(obj.adIosUrl);
//		$("#"+owner.dom_tag+"_info_iosDesc").html(obj.adIosInfo);
		$("#"+owner.dom_tag+"_div_info").window("open").window("hcenter");
		if(obj.file_ios==null) {
			$.ajax({
				type : "get",
				url : "/manager/business/adad/files",
				cache : false,
				dataType : "json",
				data: {
					adId: adId
				}
			}).done(function(result){
				if(result.isTrue) {
					obj.file_ios = result.tag[10]==null?"/resource/images/bg.png":result.tag[10];
//					obj.file_android = result.tag[20]==null?"/resource/images/bg.png":result.tag[20];
					owner.map_data[owner.currentId] = obj;
					owner.showPreView();
				} else {
					$.messager.alert("提示信息", result.message);
				}
			}).fail(function(){
				$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
			});
		} else {
			owner.showPreView();
		}
	};
	
	owner.showPreView = function(){
		var obj = owner.map_data[owner.currentId];
		$("#"+owner.dom_tag+"_preview_ios").css("background", "url(/resource/images/pv/10_"+obj.adPosition+".png) no-repeat")
			.css("background-size", "cover");
//		$("#"+owner.dom_tag+"_preview_android").css("background", "url(/resource/images/pv/20_"+obj.adPosition+".png) no-repeat");
		if(obj.adPosition=="level-10") {
			$("#"+owner.dom_tag+"_preview_ios").html("<img src=\""+obj.file_ios+"\" style=\"border:0; width:500px; height:890px; margin:0px;\" />");
//			$("#"+owner.dom_tag+"_preview_android").html("<img src=\""+obj.file_android+"\" style=\"width:240px; height:410px; margin-top:17px;\" />");
		} else if(obj.adPosition=="level-20") {
			$("#"+owner.dom_tag+"_preview_ios").html("<img src=\""+obj.file_ios+"\" style=\"border:0; width:500px; height:250px; margin-top:85px;\" />");
//			$("#"+owner.dom_tag+"_preview_android").html("<img src=\""+obj.file_android+"\" style=\"width:240px; height:134px; margin-top:48px;\" />");
		} else if(obj.adPosition=="level-30") {
			$("#"+owner.dom_tag+"_preview_ios").html("<img src=\""+obj.file_ios+"\" style=\"border:0; width:500px; height:110px; margin-top:715px;\" />");
//			$("#"+owner.dom_tag+"_preview_android").html("<img src=\""+obj.file_android+"\" style=\"width:240px; height:48px; margin-top:339px;\" />");
		} else if(obj.adPosition=="level-40") {
			$("#"+owner.dom_tag+"_preview_ios").html("<img src=\""+obj.file_ios+"\" style=\"border:0; width:500px; height:145px; margin-top:450px;\" />");
//			$("#"+owner.dom_tag+"_preview_android").html("<img src=\""+obj.file_android+"\" style=\"width:240px; height:48px; margin-top:339px;\" />");
		} else {
			$.messager.alert('提示信息', "暂无预览功能！");
		}
	};
	
	owner.changePreView = function(type){
		var file = document.getElementById(owner.dom_tag+"_info_"+type+"_upload").files[0];
		if(file) {
			var reader = new FileReader();
			reader.onload = function(img){
				$("#"+owner.dom_tag+"_preview_"+type).find("img").eq(0).attr("src", img.target.result);
			};
			reader.readAsDataURL(file);
		}
		
	};
	
	owner.upload = function(type, dom){
        if($("#"+owner.dom_tag+"_info_"+type+"_upload_ipt").filebox("getValue")=="") {
            $.messager.alert("提示信息", "请先选择需要上传的图片！");
            return;
        }
		dom.linkbutton("disable");
		$.upload({
			url: "/manager/business/adad/upload",
			domId: owner.dom_tag+"_info_"+type+"_upload",
			dataType: 'html',
			params: {
				adId: owner.currentId,
				type: type=="ios"?10:20
			},
			onComplate: function(result) {
				if(result.isTrue) {
					owner.map_data[owner.currentId]["file_"+type] = result.tag;
					$.messager.alert("提示信息", "上传成功！");
				} else {
					$.messager.alert("提示信息", result.message);
				}
				$("#"+owner.dom_tag+"_info_"+type+"_upload_ipt").filebox("clear");
				dom.linkbutton("enable");
			},
			onError: function() {
				$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
			}
		});
//		
//		$.ajaxFileUpload({
//			type:"post",
//			url:"/manager/business/adad/upload",
//			cache : false,
//			dataType: "json",		
//			secureuri: false,
//			fileElementId:""+owner.dom_tag+"_info_"+type+"_upload",
//			data: {
//				adId: owner.currentId,
//				type: type=="ios"?10:20
//			},		
//			success: function (result, status) {
//				if(result.isTrue) {
//					owner.map_data[owner.currentId]["file_"+type] = result.tag;
//					$.messager.alert("提示信息", "上传成功！");
//				} else {
//					$.messager.alert("提示信息", result.message);
//				}
//			},
//			error: function (data, status, e) {
//				$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
//			},
//			complete: function(){
//				$("#"+owner.dom_tag+"_info_"+type+"_upload_url").val("");
//				dom.linkbutton("enable");
//			}
//		});
	};
	
	
	
	
	
	
	
	
	
	
};